<?php
/*
+------------------------------------------------------------------------------+
| Copyright (c) 2009, draftrack                                          |
| All rights reserved.                                                         |
|                                                                              |
| Redistribution and use in source and binary forms, with or without           |
| modification, are permitted provided that the following conditions are met:  |
|     * Redistributions of source code must retain the above copyright         |
|       notice, this list of conditions and the following disclaimer.          |
|     * Redistributions in binary form must reproduce the above copyright      |
|       notice, this list of conditions and the following disclaimer in the    |
|       documentation and/or other materials provided with the distribution.   |
|     * Neither the name of the <organization> nor the                         |
|       names of its contributors may be used to endorse or promote products   |
|       derived from this software without specific prior written permission.  |
|                                                                              |
| THIS SOFTWARE IS PROVIDED BY draftrack team ''AS IS'' AND ANY                |
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED    |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE       |
| DISCLAIMED. IN NO EVENT SHALL draftrack team BE LIABLE FOR ANY               |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND  |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS|
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                 |
+------------------------------------------------------------------------------+
*/
/*
* Front end plugin extension to manage application initialization
*/
require_once('Zend/Controller/Plugin/Abstract.php');

/**
 * Plugin d'initialisation
 * 
 * @uses Zend_Controller_Plugin_Abstract
 */
class Myzen_Controller_Plugin_Log extends Zend_Controller_Plugin_Abstract
{
 
  /**
   * Construct
   *
  **/
	public function __construct()
	{
		$this->_initLog();
	}
	
  /**
   * Interruption avant le début du routage de la requête
   * A lire : http://framework.zend.com/manual/fr/zend.controller.plugins.html
   * 
   * @param  Zend_Controller_Request_Abstract $request
   * @return void
   */
  protected function _initLog()
  {
	$dt_appLog = new Zend_Log();
	//We can t log until the end that somthing did not work
	$dt_logCreationIssueOccured = false;
	$dt_config = Zend_Registry::get('dt_config');
	
	//logs are disabled so we set a null writer so all the logs are sent to nowhere
	if($dt_config->log->enable == false) 
	{
		$dt_nullWriter = new Zend_Log_Writer_Null;
		$dt_appLog->addWriter($dt_nullWriter);
	}	
	else 
	{
		//user requests logs to be sent to a log file 
		if( isset($dt_config->log->text_log) && isset($dt_config->log->text_log->log_stream) ) {
			//we check that the log destination is writable
				try {    			//We create the writer
    			$dt_textWriter = new Zend_Log_Writer_Stream($dt_config->log->text_log->log_stream);
				
				//We check if a particular filter needs to be applied
				if( isset($dt_config->log->text_log->filter_priority) && is_numeric($dt_config->log->text_log->filter_priority) ) {
					$dt_filterPriority = (int) $dt_config->log->text_log->filter_priority;
					$dt_textFilter = new Zend_Log_Filter_Priority($dt_filterPriority);	
					$dt_textWriter->addFilter($dt_textFilter);
				}
				//We add the writer to the logs
				$dt_appLog->addWriter($dt_textWriter);
				
				} 	catch (exception $e) {
					//We just add a log at the end to say that somethng did not work
					$dt_logCreationIssueOccured = true;
				}
			}//end text_log writer
	
		//user wants to send the logs to firebug (check http://framework.zend.com/manual/en/zend.log.writers.html#zend.log.writers.firebug for details)
		if(isset($dt_config->log->firebug_log->enable) && $dt_config->log->firebug_log->enable == true) {
			
				try {
				$dt_firebugWriter = new Zend_Log_Writer_Firebug();
			
				//We check if a particular filter needs to be applied
				if( isset($dt_config->log->firebug_log->filter_priority) && is_numeric($dt_config->log->firebug_log->filter_priority) ) {
					$dt_filterPriority = (int) $dt_config->log->firebug_log->filter_priority;
					$dt_textFilter = new Zend_Log_Filter_Priority($dt_filterPriority);	
					$dt_firebugWriter->addFilter($dt_textFilter);
				}
			
				//We add the writer to the logs
				$dt_appLog->addWriter($dt_firebugWriter);
				
				} 	catch (exception $e) {
					$dt_logCreationIssueOccured = true;			
				}
		} //end firebug logging
	} //end log writer creation


	//Check if there is at least one writer that succeeded, if not we create a mock one
	try {
		$dt_appLog->log('Check that writters are available', Zend_Log::DEBUG);
	} catch (exception $e) {
		//No writter is available, we create a mock one
		$dt_nullWriter = new Zend_Log_Writer_Null;
		$dt_appLog->addWriter($dt_nullWriter);
	}

	//If an issue occured on one of the writers, we log it
	if($dt_logCreationIssueOccured) {
		$dt_appLog->log('An issue occured while creating the different logs, you should have a look at the log initialisation', Zend_Log::ERR);	
	}

	//We set them as global variables to be accessible inside classes
	Zend_Registry::set('dt_log', $dt_appLog);

  }
  
  public function routeShutdown(Zend_Controller_Request_Abstract $request) {
	Zend_Registry::get('dt_log')->log('request routed to:'.$request->getModuleName().'|'.$request->getControllerName().'|'.$request->getActionName(),6);
	$params = $request->getParams();
	$paramString = '';
	if(!empty($params)) {
		foreach($params as $key => $value) $paramString .= $key.':'.$value.'|';
	}	
  	Zend_Registry::get('dt_log')->log('parameters of the request:'.$paramString,6);
  }
  
 
} // end Log Plugin